<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.orderSn" placeholder="订单序列号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
<!--        <el-button v-if="isAuth('sys:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
        <!-- <el-button v-if="isAuth('sys:user:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      row-key="id"
      v-loading="dataListLoading"
      style="width: 100%;">
<!--      <el-table-column-->
<!--        type="selection"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        width="50">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="200"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="orderSn"
        header-align="center"
        align="center"
        width="300"
        label="订单序列号">
      </el-table-column>
      <el-table-column
        prop="productCount"
        header-align="center"
        align="center"
        label="秒杀数量">
      </el-table-column>
      <el-table-column
        prop="payAmount"
        header-align="center"
        align="center"
        width="150"
        label="支付金额">
      </el-table-column>
      <el-table-column
        prop="productName"
        header-align="center"
        align="center"
        label="产品名称"
        width="200">
      </el-table-column>
      <el-table-column
        prop="sessionName"
        header-align="center"
        align="center"
        width="180"
        label="场次名称">
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        width="180"
        label="用户ID">
      </el-table-column>
      <el-table-column
        prop="gmtCreate"
        header-align="center"
        align="center"
        width="180"
        label="秒杀时间">
      </el-table-column>
      <el-table-column
        prop="gmtModified"
        header-align="center"
        align="center"
        width="180"
        label="支付时间">
      </el-table-column>

      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:user:add') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="addProductHandle(scope.row)">添加秒杀产品</el-button>
          <el-button v-if="isAuth('sys:user:update') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('sys:user:update')" type="text" size="small" @click="addProductHandle(scope.row, 'detail')">详情</el-button>

          <el-button v-if="isAuth('sys:user:delete') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="deleteHandle(scope.row.id, scope.row.sessionName)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 秒杀活动：新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 秒杀产品：新增 / 修改 -->
    <add-product v-if="addProductVisible" ref="addProduct" @refreshDataList="getDataList"></add-product>

  </div>
</template>

<script>
  import AddOrUpdate from './seckill-add.vue'
  import addProduct from './seckill-add-product.vue'
  export default {
    data () {
      return {
        dataForm: {
          orderSn: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addProductVisible: false
      }
    },
    components: {
      AddOrUpdate,
      addProduct
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        console.log('orderSN标识:', this.dataForm.orderSn)
        this.$http({
          url: this.$http.adornUrl('/sys/app/user/listOrderForAdmin'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'orderSn': this.dataForm.orderSn
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.dataList.map((item, i) => {
              item.hasChildren = true
            })
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 秒杀活动：新增 / 修改
      addOrUpdateHandle (id, type) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, type)
        })
      },
      // 秒杀产品：新增 / 修改
      addProductHandle (row, type) {
        this.addProductVisible = true
        this.$nextTick(() => {
          this.$refs.addProduct.init(row, type)
        })
      },
      // 删除
      deleteHandle (id, sessionName) {
        var sessionNames = sessionName ? [sessionName] : this.dataListSelections.map(item => {
          return item.sessionName
        })
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[场次名称=${sessionNames.join(',')}]进行[${sessionNames ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/coupon/coupon/session/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
      // expandChange(tree, treeNode, resolve) {
      //   setTimeout(() => {
      //     resolve([
      //       {
      //         id: 31,
      //         date: '2016-05-01',
      //         name: '王小虎',
      //         address: '上海市普陀区金沙江路 1519 弄'
      //       }, {
      //         id: 32,
      //         date: '2016-05-01',
      //         name: '王小虎',
      //         address: '上海市普陀区金沙江路 1519 弄'
      //       }
      //     ])
      //   }, 1000)
      // }
    }
  }
</script>
